/*
 * @Description:
 * @Author: qingyang
 * @Date: 2023-07-20 11:44:32
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-26 15:31:35
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getConfigList } from '@/apis/eci/dataManagement/config'
import type { pageConfigParams } from '@/apis/eci/dataManagement/model/configModel'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import type { SearchOptionsType } from '@/components/CPageTable'

export const useFactorList = () => {
  // 搜索字段及其他
  const searchForm = reactive<pageConfigParams>({
    configName: undefined,
    creator: undefined,
    status: undefined,
    createTime: undefined
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '配置主题',
      field: 'configName',
      type: 'input'
    },
    {
      name: '发起人',
      field: 'creator',
      type: 'input'
    },
    {
      name: '状态',
      field: 'status',
      type: 'select',
      fixedOption: 'configStatusTypeOption'
    },
    {
      name: '更新时间',
      field: 'createTime',
      type: 'datePicker',
      startPlaceholder: '更新起始时间',
      endPlaceholder: '更新截止时间',
      dateType: 'datetimerange',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    selection: false,
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: pageConfigParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getConfigList(params)
  }

  // 处理参数
  const handleParams = (): pageConfigParams => {
    // 参数
    let params: pageConfigParams = {
      configName: searchForm.configName,
      creator: searchForm.creator,
      status: searchForm.status,
      updateTimeStart: searchForm.createTime?.length ? searchForm.createTime[0] : undefined,
      updateTimeEnd: searchForm.createTime?.length ? searchForm.createTime[1] : undefined
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: any[] = [
    {
      title: '序号',
      type: 'seq',
      width: 60
    },
    {
      title: '配置主题',
      field: 'configName',
      slot: 'configName',
      minWidth: 150
    },
    {
      title: '发起人',
      field: 'creator'
    },
    {
      title: '状态',
      slot: 'statusSlot',
      width: 100
    },
    {
      title: '填报周期',
      field: 'fillPeriod',
      width: 100
    },
    {
      title: '填报频次',
      field: 'fillFrequency',
      slot: 'fillFrequency',
      width: 100
    },
    {
      title: '更新时间',
      field: 'updateTime',
      width: 180
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 150,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
